package com.lee.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.lee.ssm.domain.Product;
import com.lee.ssm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService iProductService;

//    @RequestMapping("/findAll")
//    public ModelAndView findAll() throws Exception {
//        ModelAndView mv=new ModelAndView();
//        System.out.println("111");
//       List<Product> all = iProductService.findAll();
//       mv.setViewName("product-list");
//       mv.addObject("productList",all);
//        return mv;
//    }
    @RequestMapping("/findAll")
    public ModelAndView findAll(@RequestParam(name="page",required = true,defaultValue ="1" ) String page,@RequestParam(name = "size",required = true,defaultValue = "4")String size) throws Exception {
        ModelAndView mv=new ModelAndView();

        List<Product> all = iProductService.findAll(page,size);
        PageInfo pageInfo=new PageInfo(all);
        mv.setViewName("product-list");
        mv.addObject("pageInfo",pageInfo);
        return mv;
    }

    @RequestMapping("/save")
    public String save(Product product){

        iProductService.save(product);

        return "redirect:findAll.do";
    }

}
